<script setup>
import {
    Edit, Delete, Position, Upload
} from '@element-plus/icons-vue'

import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
    addcourseservice, getpartpathservice, getalljobsservice, getpartcourseservice,
    updatepathservice, deletepathservice, deletecourseservice, updatecourseservice,
    getareaservice,
} from '@/api/book.js'

//控制添加分类弹窗
const dialogVisible = ref(false)
//分页条数据模型
//const pageNum = ref(1)//当前页
const total = ref(20)//总条数
//const pageSize = ref(3)//每页条数
//文章分类数据模型

//定义变量，控制标题
const title = ref('')

//交通路线列表数据模型
const courses = ref([])

//与后端交互的json数据
const pathmodel = ref({
    name: null,
    sex: null,
    birthday: null,
    areaid: null,
    phone: null,
    type: null,
    comment: null
})

const findmodel = ref({
    pageNum: 1,
    pageSize: 5,
    str: '',
})
const areas = ref([]);
const getareas = async () => {
    let res = await getareaservice({
        pageNum: 1,
        pageSize: 1000,
        str: '',
    })
    areas.value = res.data.items
}
getareas();
const stafflist = async () => {
    console.log(findmodel.value);
    let result = await getpartcourseservice(findmodel.value);

    console.log(result);
    total.value = result.data.total;
    courses.value = result.data.items;
}
stafflist();
//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    findmodel.value.pageSize = size;
    stafflist();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    findmodel.value.pageNum = num;
    stafflist();
}
const cleardata = () => {
    findmodel.value.str = ''
}
//表单校验
const rules = {
    name: [
        { required: true, message: '请输入姓名', trigger: 'blur' },
    ],
    sex: [
        { required: true, message: '请选择性别', trigger: 'blur' },
    ],
    birthday: [
        { required: true, message: '请选择生日', trigger: 'blur' },
    ],
    areaid: [
        { required: true, message: '请选择负责区域', trigger: 'blur' },
    ],
    type: [
        { required: true, message: '请选择职务', trigger: 'blur' },
    ],
    comment: [
        { required: true, message: '请输入备注', trigger: 'blur' },
    ],
    phone: [
        { required: true, message: '请输入联系方式', trigger: 'blur' },
    ],

}
const add = () => {
    pathmodel.value.name = null
    pathmodel.value.sex = null
    pathmodel.value.birthday = null
    pathmodel.value.areaid = null
    pathmodel.value.type = null
    pathmodel.value.comment = null
    pathmodel.value.phone = null
    dialogVisible.value = true;
    title.value = '添加人员'
}
const formref = ref(null); //表单校验
const addstaff = async () => {
    formref.value.validate(async (valid) => {
        if (!valid) {
            ElMessage.error('信息填写不完整！');
        }
        else {
            console.log(pathmodel.value);
            let result = await addcourseservice(pathmodel.value);
            console.log(result);
            if (result.code == 0) {
                ElMessage.success("添加成功")
                dialogVisible.value = false;
                stafflist();
            }
            else ElMessage.error(result.message ? result.message : '添加失败')
        }
    });
}
const updatestaff = async () => {
    formref.value.validate(async (valid) => {
        if (!valid) {
            ElMessage.error('信息填写不完整！');
        }
        else {
            let result = await updatecourseservice(pathmodel.value);
            if (result.code == 0) {
                ElMessage.success("修改成功")
                dialogVisible.value = false;
                stafflist();
            }
            else ElMessage.error(result.message ? result.message : '修改失败')
        }
    });
}
//删除分类
const del = (row) => {
    ElMessageBox.confirm(
        '你确认删除该条信息吗？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //用户点击了确认
            let result = await deletecourseservice(row.id)
            if (result.code != 0) ElMessage.error(result.message ? result.message : '删除失败')
            else ElMessage.success('删除成功')
            //再次调用getAllCategory，获取所有文章分类
            stafflist();
        })
        .catch(() => {
            //用户点击了取消
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}
//展示编辑弹窗
const showDialog = (row) => {
    title.value = '编辑人员';
    dialogVisible.value = true;
    //数据拷贝
    pathmodel.value.account = row.account
    pathmodel.value.pass = row.pass
    pathmodel.value.name = row.name
    pathmodel.value.sex = row.sex
    pathmodel.value.birthday = row.birthday
    pathmodel.value.areaid = row.areaid
    pathmodel.value.type = row.type
    pathmodel.value.phone = row.phone
    pathmodel.value.comment = row.comment
    //扩展i的属性，将来需要传递给后台，完成分类的修改
    pathmodel.value.id = row.id;
    console.log(row.id);
}

const alltype = ref(['销售员工'])

</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>营销人员管理</span>
                <div class="extra">
                    <el-button type="primary" @click="add">添加人员</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form>
            <el-form-item label="查询条件:">
                <el-input v-model="findmodel.str" placeholder="请输入查询内容" style="width: 300px"></el-input>
                <el-button type="primary" @click="stafflist" style="margin-left: 15px;">搜索</el-button>
                <el-button @click="cleardata">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 文章列表 -->
        <el-table :data="courses" style="width: 100%">
            <el-table-column label="姓名" prop="name"></el-table-column>
            <el-table-column label="性别" prop="sex"></el-table-column>
            <el-table-column label="生日" prop="birthday"></el-table-column>
            <el-table-column label="负责区域" prop="areaname"></el-table-column>
            <el-table-column label="职务" prop="type"></el-table-column>
            <el-table-column label="联系方式" prop="phone"></el-table-column>
            <el-table-column label="备注" prop="comment"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="del(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="findmodel.pageNum" v-model:page-size="findmodel.pageSize"
            :page-sizes="[5, 10, 15, 20]" layout="jumper, total, sizes, prev, pager, next" background :total="total"
            @size-change="onSizeChange" @current-change="onCurrentChange"
            style="margin-top: 20px; justify-content: flex-end" />
    </el-card>
    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="28%">
        <el-form :model="pathmodel" ref="formref" :rules="rules" label-width="90px" style="padding-right: 30px">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="pathmodel.name" placeholder="请输入姓名"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <el-radio label="男" v-model="pathmodel.sex">男</el-radio>
                <el-radio label="女" v-model="pathmodel.sex">女</el-radio>
            </el-form-item>
            <el-form-item label="生日" prop="birthday">
                <el-date-picker v-model="pathmodel.birthday" style="width: 350px" type="date" placeholder="请选择日期"
                    format="YYYY/MM/DD" value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="负责区域" prop="areaid">
                <el-select placeholder="请选择负责区域" clearable style="width: 350px" v-model="pathmodel.areaid">
                    <el-option v-for="c in areas" :key="c.id" :label="c.name" :value="c.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="职务" prop="type">
                <el-select placeholder="请选择职务" clearable style="width: 350px" v-model="pathmodel.type">
                    <el-option v-for="c in alltype" :key="c" :label="c" :value="c">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="联系方式" prop="phone">
                <el-input v-model="pathmodel.phone" placeholder="请输入联系方式"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="comment">
                <el-input v-model="pathmodel.comment" type="textarea" :rows="4" placeholder="请输入备注">
                </el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="title == '添加人员' ? addstaff() : updatestaff()"> 确认 </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>